﻿using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using Secured3d.Auto.Utils;

namespace Secured3d.Auto.Pages
{
    public class LoginPage : BasePage
    {
        public LoginPage()
        {
            Driver.Manage().Window.Maximize();
            Driver.Navigate().GoToUrl("http://cloud1.secured3d.com/");
        }

        public LoginCommand LoginAs(string userName)
        {
            return new LoginCommand(userName);
        }

        public RegisterCommand RegisterAs()
        {
            return new RegisterCommand();
        }

    }

    public class LoginCommand : BasePage
    {
        // Locators
        [FindsBy(How = How.Id, Using = "signinUsername")] 
        private IWebElement loginField = null;

        [FindsBy(How = How.Id, Using = "signinPassword")]
        private IWebElement passwordField = null;

        [FindsBy(How = How.Name, Using = "signIn")]
        private IWebElement loginButton = null;

        // Constructor
        public LoginCommand(string userName)
        {
            loginField.SendKeys(userName);
        }

        public LoginCommand WithPassword(string password)
        {
            passwordField.SendKeys(password);
            return this;
        }

        public UserProfilePage Login()
        {
            loginButton.Click();
            return new UserProfilePage();
        }
    }

    public class RegisterCommand : BasePage
    {
        //locators
        [FindsBy(How = How.Id, Using = "ui-id-2")]
        private IWebElement signUpLink = null;


        [FindsBy(How = How.Id, Using = "firstname")] 
        private IWebElement firstNameField = null;

        [FindsBy(How = How.Id, Using = "lastname")]
        private IWebElement lastNameField = null;

        [FindsBy(How = How.Id, Using = "organization")]
        private IWebElement organizationField = null;

        [FindsBy(How = How.Id, Using = "signupUsername")]
        private IWebElement emailField = null;

        [FindsBy(How = How.Id, Using = "signupPassword")]
        private IWebElement passwordField = null;

        [FindsBy(How = How.Id, Using = "confirm_password")]
        private IWebElement repeatPasswordField = null;

        [FindsBy(How = How.Id, Using = "captcha")]
        private IWebElement capchaField = null;

        [FindsBy(How = How.Name, Using = "signUp")]
        private IWebElement signUpButton = null;

        public RegisterCommand()
        {
            signUpLink.WaitUntilVisible().Click();
        }

        public RegisterCommand FirstName(string firstName)
        {
            firstNameField.SendKeys(firstName);
            return this;
        }

        public RegisterCommand LastName(string lastName)
        {
            lastNameField.SendKeys(lastName);
            return this;
        }

        public RegisterCommand Organization(string organization)
        {
            organizationField.SendKeys(organization);
            return this;
        }

        public RegisterCommand Email(string email)
        {
            emailField.SendKeys(email);
            return this;
        }

        public RegisterCommand Password(string password)
        {
            passwordField.SendKeys(password);
            repeatPasswordField.SendKeys(password);
            return this;
        }

        public UserProfilePage Register()
        {
            signUpButton.Click();

            return new UserProfilePage();
        }
    }
}
